/*
 * @Description: 
 * @Version: 2.0
 * @Author: AICHEN
 * @Date: 2023-02-10 01:06:57
 * @LastEditors: AICHEN
 * @LastEditTime: 2023-02-26 19:06:25
 */
import axios from 'axios';
import type { Method } from 'axios';
import {  ApiResponse } from './types';

// 申请一个请求实例
const service=axios.create({
    baseURL:"http://127.0.0.1:8080",
    timeout:10000,
    headers:{
        "Content-Type":"application/json;charset=utf-8"
    },
})


// 响应拦截
service.interceptors.response.use(res=>{

    const {code}=res.data;

    if(code>=400){
        console.log(res.data.message);
        return Promise.reject(res.data);
    }
    return res.data
})

const request=<T>(url:string,method:Method,data?:Object|string)=>{
    return service.request<T,ApiResponse<T>>({
        url,
        method,
        [method.toLocaleLowerCase()==="get" ? "params" : "data"]:data
    });
}

export default request;